package core;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import ui.MainWindow;
//import parsing.User;

public class Group {

	private static int counterID = 1;
	private String groupName;
	private int groupId;
	private int numOfQuestions; //Default 100
	private int contID; //Continent
	private int countryID; 
	private Set<User> groupUsers;
	private List<City> questions;
	private int score; //on the fly value for user
	
	/* group id- 1-n, n number of groups */
	
	// Constructors	
	public Group(int ID, String name, int numOfQuestions, int countryID, int contID) {
		this.groupName = name;
		this.groupId = ID;
		this.numOfQuestions=numOfQuestions;
		this.groupUsers=new TreeSet<User>();
		this.contID = contID ;
		this.countryID = countryID;
		questions = new ArrayList<City>();	
	}
	
	public Group(String name, int numOfQuestions, int countryID, int contID) {
		this(-1, name, numOfQuestions, countryID, contID);
	}
	
	public Group(String name, int numOfQuestions) {
		this(counterID, name, numOfQuestions, -1, -1);
		counterID++;
	}
	
	public Group(String name){
		this(counterID, name, 10, -1, -1);	
		counterID++;
	}
	
	
	// Methods
	
	/**
	 * load game questions from DB according to the settings	
	 */
	public void loadQuestions() {
		if( contID == -1 && countryID == -1){ //random game
			questions = MainWindow.myDB.getRandomCities(numOfQuestions);
			return;
		}
		if(contID != -1){ //countryID == -1, city by continent
			questions = MainWindow.myDB.getCitiesByContinent(contID, numOfQuestions);
		}else{ //contID == -1, cities by country
			questions = MainWindow.myDB.getCitiesByCountry(countryID, numOfQuestions);
		}
		return;
		
	}
	
	public String getName() {
		return this.groupName;
	}

	public int getId() {
		return this.groupId;
	}
	
	public int getNumOfQuestions(){
		return this.numOfQuestions;
	}
	
	public void setId(int id){
		this.groupId=id;
	}
	
	public void setName(String name){
		this.groupName=name;
	}
	
	public void setNumOfWuestions(int numOfQuestions){
		this.numOfQuestions=numOfQuestions;
	}
	
	public int addUserToGroup(User user){
		/* returns 1 on success, otherwise 0*/
		if (user!=null){
			if(!(this.groupUsers.contains(user))){
			this.groupUsers.add(user);
			return 1;
			}else{
				return 0;
			}
		}
		return 0;
	}
	
	public static int getCounterID() {
		return counterID;
	}

	public static void setCounterID(int counterID) {
		Group.counterID = counterID;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public int getContID() {
		return contID;
	}

	public void setContID(int contID) {
		this.contID = contID;
	}

	public int getCountryID() {
		return countryID;
	}

	public void setCountryID(int countryID) {
		this.countryID = countryID;
	}

	public Set<User> getGroupUsers() {
		return groupUsers;
	}
	
	public Integer getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public void setGroupUsers(Set<User> groupUsers) {
		this.groupUsers = groupUsers;
	}

	public void setQuestions(List<City> q) {
		this.questions = q;
	}
	
	public void setNumOfQuestions(int numOfQuestions) {
		this.numOfQuestions = numOfQuestions;
	}

	public void deleteUserFromGroup(User user){
	if (user!=null){
		groupUsers.remove(user);
		}
	}
	
	public City getNextQuestion(){
		//returns the last cityID in questions list if exists,
		//or null if not exists
		//removes the city from questions
		City next = null;
		if(!questions.isEmpty()){
			//get last cityID on list
			next = questions.get(questions.size() - 1);
			//remove from list
			questions.remove(questions.size() - 1);
		}
		return next;
	}
	
	public int getNumberOfQuestionsLeft() {
		return questions.size();
	}
	
}
